package com.eight.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.AclPermission;
import com.eight.common.pojo.EduCourse;
import com.eight.common.pojo.EduSubject;
import com.eight.common.vo.Result;
import com.eight.course.dao.CourseDao;
import com.eight.course.dao.SubjectDao;
import com.eight.course.service.CourseService;
import com.eight.course.service.SubjectService;
import com.eight.course.vo.CourseVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: qy8-education
 * @description:
 * @author: 张亚祥
 * @create: 2023-05-26 10:51
 **/
@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseDao courseDao;

    @Resource
    private SubjectDao subjectDao;
    @Override
    public Page<EduCourse> findPage(Integer currentSize, Integer pageSize, CourseVo couresVo) {
        Page<EduCourse> page = new Page<>(currentSize, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(couresVo.getTitle())) {
            wrapper.like("title", couresVo.getTitle());
        }
        if (StringUtils.hasText(couresVo.getStatus())) {
            wrapper.eq("status", couresVo.getStatus());
        }
        Page<EduCourse> page1 = courseDao.findCourse(page,wrapper);
        return page1;
    }

    private List<EduSubject> findChildren(EduSubject item,List<EduSubject> childrens){
        ArrayList<EduSubject> list = new ArrayList<>();
        for (EduSubject children : childrens) {
            if(item.getId().equals(children.getParentId())){
                list.add(children);
            }
        }
        for (EduSubject eduSubject : list) {
            eduSubject.setChildren(findChildren(eduSubject,childrens));
        }
        return list;
    }
        @Override
    public int updateStatus(String id, String status) {
        EduCourse course=new EduCourse();
        course.setId(id);
        course.setStatus(status);
        course.setGmtModified(LocalDateTime.now());
        int byId = courseDao.updateById(course);
        return byId;
    }

    @Override
    public int updateCourse(EduCourse eduCourse) {
        int i = courseDao.updateById(eduCourse);
        return i;
    }

    @Override
    public int updateCourseDescription(EduCourse EduCourse) {

        int i = courseDao.updateCourseDescription(EduCourse);
        return i;
    }


}
